package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
)

// LifetimeRecord 有寿维护记录
type LifetimeRecord struct {
	BaseModel
	Type         int   `orm:"column(type);description(维护任务类型)" json:"type"`
	AdjustType   int   `orm:"column(adjust_type);description(调整类型)" json:"adjustType"`
	AircraftId   int64 `orm:"column(aircraft_id);description(飞机Id)" json:"aircraftId"`
	ComponentId  int64 `orm:"column(component_id);description(飞机组件Id)" json:"componentId"`
	TaskId       int64 `orm:"column(task_id);description(任务累加时的任务Id)" json:"taskId"`
	WorkCount    int   `orm:"column(work_count);description(当前工作次数)" json:"workCount"`
	WorkDuration int   `orm:"column(work_duration);description(当前工作时长，单位秒)" json:"workDuration"`
}

func init() {
	orm.RegisterModel(new(LifetimeRecord))
}

// SimpleField 简略查询字段
func (*LifetimeRecord) SimpleField() []string {
	return []string{"id", "type"}
}

// SearchField 定义模型的可搜索字段
func (*LifetimeRecord) SearchField() []string {
	return []string{"type"}
}

// WhereField 定义模型可作为条件的字段
func (*LifetimeRecord) WhereField() []string {
	return []string{"id", "status", "type"}
}

func (m *LifetimeRecord) TableName() string {
	return "lifetime_record"
}

// AddLifetimeRecord insert a new LifetimeRecord into database and returns
// last inserted Id on success.
func AddLifetimeRecord(m *LifetimeRecord, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetLifetimeRecordById retrieves LifetimeRecord by Id. Returns error if
// Id doesn't exist
func GetLifetimeRecordById(id int64) (v *LifetimeRecord, err error) {
	o := orm.NewOrm()
	v = &LifetimeRecord{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(LifetimeRecord)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateLifetimeRecordById updates LifetimeRecord by Id and returns error if
// the record to be updated doesn't exist
func UpdateLifetimeRecordById(m *LifetimeRecord, userId int64) (err error) {
	o := orm.NewOrm()
	v := LifetimeRecord{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		m.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteLifetimeRecord deletes LifetimeRecord by Id and returns error if
// the record to be deleted doesn't exist
func DeleteLifetimeRecord(id int64) (err error) {
	o := orm.NewOrm()
	v := LifetimeRecord{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&LifetimeRecord{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroyLifetimeRecord Destroy LifetimeRecord by Id and returns error if
func DestroyLifetimeRecord(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := LifetimeRecord{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
